#include "courseitemwidget.h"
#include "qpainter.h"
#include "qevent.h"

CourseItemWidget::CourseItemWidget(QWidget *parent):QWidget(parent)
{
    this->startHour=8;              //开始小时
    this->endHour=21;                //结束小时
    this->timeleftx=120;            //时间区域的开始
    this->crsttopy=30;
    this->installEventFilter(this);
}


void CourseItemWidget::drawTeacherInfo(QPainter *painter)
{
    int m_X=0;
    int m_Y=this->crsttopy;

    QRect rectTitle(m_X,m_Y, timeleftx, this->height()-this->crsttopy);
    QColor clr(128,128,128,200);
    painter->fillRect(rectTitle,clr);

    QString strTeacherName("数学：闫老师");
    double totalcrstime=getTotalCrstime();
    QString strCrstime=QString::number(totalcrstime,'f',1);
    int textHeight= painter->fontMetrics().boundingRect(strTeacherName).height();

    m_X=5;
    m_Y+=textHeight;
    painter->drawText(m_X,m_Y,strTeacherName);
    m_Y+=15;
    painter->drawText(m_X,m_Y,"科组：奥数1班");
    m_Y+=15;
    painter->drawText(m_X,m_Y,QString("课时：%1小时").arg(strCrstime));
}

void CourseItemWidget::drawCrst(QPainter *painter)
{
    int m_Y=this->crsttopy;

    foreach (CrseItemTime cr, crslst) {
        float startX=this->getX(cr.StartTime);
        float endX=this->getX(cr.EndTime);
        QRectF rectcr(startX,m_Y,endX-startX,this->height()-m_Y);

        drawCrstItem(painter,rectcr);
    }
}

void  CourseItemWidget::drawCrstItem(QPainter *painter,QRectF rectcr)
{
    int size=16,padding=2;
    QColor clr(32,32,32,200);
    QRectF rectClose(rectcr.right()-size/2,rectcr.top()-size/2,size,size);
    painter->fillRect(rectcr,clr);
    painter->drawEllipse(rectClose);
    painter->drawLine(rectClose.left()+padding,rectClose.top()+padding,rectClose.right()-padding,rectClose.bottom()-padding);
    painter->drawLine(rectClose.left()+padding,rectClose.bottom()-padding,rectClose.right()-padding,rectClose.top()+padding);
}

int CourseItemWidget::getEnterCrstIndex(QPoint *point)
{
    int m_Y=this->crsttopy;
    int index=0;
    bool blFind=false;
    foreach (CrseItemTime cr, crslst) {
        float startX=this->getX(cr.StartTime);
        float endX=this->getX(cr.EndTime);
        QRectF rectcr(startX,m_Y,endX-startX,this->height()-m_Y);

        int size=16;
        QRectF rectClose(rectcr.right()-size/2,rectcr.top()-size/2,size,size);
        if(rectClose.contains(point->x(),point->y()))
        {
            blFind=true;
            break;
        }
        index++;
    }

    if(blFind)
        return index;
    return -1;
}

double CourseItemWidget::getTotalCrstime()
{
    double total=0;
    foreach (CrseItemTime cr, crslst) {
        total+=(cr.EndTime-cr.StartTime);
    }

    return total;
}

float CourseItemWidget::getClosedtoTime(float time)
{
    int totalCount=this->endHour-this->startHour;
    float arr[totalCount];
    for(int i=0;i<totalCount;i++)
    {
        arr[i]=this->startHour+i*0.5;
        float diff=qAbs(time-arr[i]);
        if(diff<0.2)
        {
            time=arr[i];
            break;
        }
    }

    return time;
}


void CourseItemWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing);

    drawTeacherInfo(&painter);
    drawCrst(&painter);

    int maxCount=this->endHour-this->startHour;
    int m_X=this->timeleftx,m_Y=0;
    int spanWidth=(this->width()-m_X)/maxCount;
    int spanHeight=this->height()-this->crsttopy;
    int hour=8;
    for(int i=0;i<maxCount;i++)
    {
        m_X=this->timeleftx+spanWidth*i;
        QRectF rectCrs(m_X,m_Y+this->crsttopy,spanWidth,spanHeight);
        painter.drawRect(rectCrs);

        QString text=QString("%1:30").arg(hour);
        QRect textRect=painter.fontMetrics().boundingRect(text);
        int temp=spanWidth/2-textRect.width()/2;
        m_X=this->timeleftx+spanWidth*i+temp;
        QRectF rect(m_X,m_Y,spanWidth,spanHeight);
        painter.drawText(rect,text);

        hour++;
    }

    bool isFlag=false;
    QRectF rectStart;
    m_X=0; m_Y=this->crsttopy;
    for(int i=0;i<maxCount;i++)                       //按最小单位半小时调整区域
    {
        m_X=this->timeleftx+ spanWidth*i;

        QRectF rect(m_X,m_Y,spanWidth,spanHeight);
        m_X=rect.left()+rect.width()/2;
        QPen pold=painter.pen();
        QPen p;
        QVector<qreal> dashes;
        dashes<<3<<5;
        p.setDashPattern(dashes);
        p.setColor(Qt::white);
        painter.setPen(p);
        painter.drawLine(m_X,rect.top(),m_X,rect.bottom());
        painter.setPen(pold);


        if(isPressed)
        {
            if(rect.contains(lastPoint))
            {
                isFlag=true;
                isDraging=true;
                if(lastPoint.x()>rect.left()+rect.width()/2)
                {
                    rectStart=QRectF(rect.x()+spanWidth/2,rect.top(),rect.width()/2,rect.height());
                }
                else
                {
                    rectStart=QRectF(rect.x(),rect.top(),rect.width(),rect.height());
                }

                dragStarttime=getTime(rectStart.x());
            }
        }
    }

    if(isFlag)
    {
        QRectF rectcr(rectStart.left(),rectStart.top(),currentPoint.x()-rectStart.x(),rectStart.height());
        drawCrstItem(&painter,rectcr);
    }
}

float CourseItemWidget::getTime(int mouseX)
{
    int maxCount=this->endHour-this->startHour;
    int totalWidth=this->width()-this->timeleftx;
    int spanX=mouseX-timeleftx;
    float ftime=this->startHour+spanX*1.0f/totalWidth*maxCount;
    return ftime;
}

float CourseItemWidget::getX(float time)
{
    int maxCount=this->endHour-this->startHour;
    int m_X=this->timeleftx;
    int totalWidth=this->width()-this->timeleftx;
    float spantime=time-this->startHour;
    float spanX=this->timeleftx+ spantime/maxCount*totalWidth;
    return spanX;
}

bool CourseItemWidget::eventFilter(QObject *watched, QEvent *event)
{
    QMouseEvent *mouseEvent=static_cast<QMouseEvent *>(event);
    if(event->type()==QEvent::MouseButtonPress)
    {
        if(mouseEvent->button()==Qt::LeftButton)
        {
            lastPoint=mouseEvent->pos();

            int index=getEnterCrstIndex(&lastPoint);
            if(index>-1)
            {
                this->crslst.removeAt(index);
                this->update();
            }
            else
            {
                isPressed=true;
            }
        }
    }
    else if(event->type()==QEvent::MouseMove)
    {
        if(isPressed)
        {
            currentPoint=mouseEvent->pos();
            if(isDraging)
            {
                dragEndtime=getTime(currentPoint.x());
            }
            this->update();
        }
    }
    else if(event->type()==QEvent::MouseButtonRelease)
    {
        isPressed=false;
        if(isDraging&&dragEndtime>dragStarttime)
        {
            dragEndtime=getClosedtoTime(dragEndtime);
            CrseItemTime crsetime;
            crsetime.StartTime=dragStarttime;
            crsetime.EndTime=dragEndtime;
            crslst.append(crsetime);

            this->update();
        }

        dragStarttime=dragEndtime=0;
        lastPoint=QPoint();
        isDraging=false;
    }

    return QWidget::eventFilter(watched,event);
}
